/**
 * 滚动条宽度
 */
var scrollbarWidth = (function () {
  var width;
  var div = document.createElement('div');
  div.style['overflow'] = 'scroll';
  div.style['width'] = '100px';
  div.style['height'] = '100px';
  document.body.appendChild(div);
  width = div.offsetWidth - div.clientWidth;
  document.body.removeChild(div)
  return width;
}())
/**
 * 获取元素的最小宽度。
 * @param {Element} el  元素
 */
var getElmentMinWidth = function (el) {
  var div = document.createElement("div");
  div.style["overflow"] = "scroll";
  div.style["width"] = "0px";
  div.style["height"] = "0px";
  div.appendChild(el.cloneNode(true));
  document.body.appendChild(div); //firefox 滚动宽度和chrome scrollWidth 不一致。
  var minWidth = div.scrollWidth;
  document.body.removeChild(div);
  return minWidth;
}

var arrayToggle = function (arr, value) {
  var index = arr.indexOf(value);
  if (index == -1) {
    arr.push(value)
  } else {
    arr.splice(index, 1)
  }
}

var arrayRemove = function (arr, item) {
  var index = arr.indexOf(item);
  arr.splice(index, 1)
  return index;
}

var toArray = function (arrlike) {
  return Array.prototype.slice.call(arrlike);
}
var getElementPosition = function (el) {
  // console.log(el.getBoundingClientRect());
  var top = el.offsetTop,
    left = el.offsetLeft;
  while (el = el.offsetParent) {
    top += el.offsetTop;
    left += el.offsetLeft;
  }
  return {
    top,
    left
  }
}
var addWheelListener = (function () {
  var prefix = "",
    _addEventListener = 'addEventListener',
    _removeEventListener = 'removeEventListener',
    support;
  // detect event model
  //  if ( window.addEventListener ) {
  //      _addEventListener =  "addEventListener ";
  //  } else {
  //      _addEventListener =  "attachEvent ";
  //      prefix =  "on ";
  //  }
  // detect available wheel event
  support = "onwheel" in document.createElement("div") ? "wheel" : // Modern browsers support  "wheel "
    document.onmousewheel !== undefined ? "mousewheel" : // Webkit and IE support at least  "mousewheel "
    "DOMMouseScroll"; // let's assume that remaining browsers are older Firefox

  return function (elem, callback, useCapture) {

    // handle MozMousePixelScroll in older Firefox
    if (support == "DOMMouseScroll") {
      return _addWheelListener(elem, "MozMousePixelScroll", callback, useCapture);
    } else {
      return _addWheelListener(elem, support, callback, useCapture);
    }
  };

  function _addWheelListener(elem, eventName, callback, useCapture) {
    var backfn = (support == "wheel" ? callback : function (originalEvent) {
      !originalEvent && (originalEvent = window.event);
      // create a normalized event object
      var event = {
        // keep a ref to the original event object
        originalEvent: originalEvent,
        target: originalEvent.target || originalEvent.srcElement,
        type: "wheel",
        deltaMode: originalEvent.type == "MozMousePixelScroll" ? 0 : 1,
        deltaX: 0,
        deltaY: 0,
        deltaZ: 0,
        preventDefault: function () {
          originalEvent.preventDefault ?
            originalEvent.preventDefault() :
            originalEvent.returnValue = false;
        }
      };
      // calculate deltaY (and deltaX) according to the event
      if (support == "mousewheel") {
        event.deltaY = -1 / 40 * originalEvent.wheelDelta;
        // Webkit also support wheelDeltaX
        originalEvent.wheelDeltaX && (event.deltaX = -1 / 40 * originalEvent.wheelDeltaX);
      } else {
        event.deltaY = originalEvent.deltaY || originalEvent.detail;
      }
      // it's time to fire the callback
      return callback(event);
    })
    elem[_addEventListener](prefix + eventName, backfn, useCapture || false);
    return function () {
      elem[_removeEventListener](prefix + eventName, backfn, useCapture || false);
    }
  }
}());

var addWindowResize = (function () {
  var fns = [];
  var old = window.onresize;

  window.onresize = function () {
    if (old) {
      old.apply(window, toArray(arguments));
    }
    fns.forEach((fn) => {
      fn()
    })
  }
  return function (fn) {
    fns.push(fn);
    return function () {
      arrayRemove(fns, fn)
    }
  }
}());

var eventTriggerOnce = function (ignores, callback, eventName = 'click') {
  var temp = function (ev) {
    var isTrigger = true,
      tar = ev.target;
    if (ignores.contains(tar)) {
      isTrigger = false;
    }

    if (isTrigger) {
      callback(ev)
      document.removeEventListener(eventName,temp,true);
    }
  }
  document.addEventListener(eventName,temp,true);

  return function(){
    document.removeEventListener(eventName,temp,true);
  }
}

export {
  scrollbarWidth,
  getElmentMinWidth,
  getElementPosition,
  arrayToggle,
  toArray,
  addWheelListener,
  addWindowResize,
  eventTriggerOnce
}
